﻿using System.Data;
using System.Data.SqlClient;

namespace Adams.Web.Data.News
{
    public static class Article
    {
        public static Models.News.Article.PageLoad PageLoad(int id)
        {
            var data = new Models.News.Article.PageLoad();

            using (SqlDataReader dr = DB.RunDataReader(@"
SELECT P.* 
FROM News.News P
WHERE P.ArticleID = @ArticleID
                ", 
                CommandType.Text,
                DB.CreateParameter("@ArticleID", id)))
            {
                if (dr.HasRows)
                {
                    dr.Read();
                    data.Article = DB.InitFromReader<Shared.Entities.News.Article>(dr, new ColMap { NameInDb = "ArticleID", NameInObject = "ID" });
                }

                return data;
            }
        }

        public static Models.News.Article.PageLoad PageLoad(string title)
        {
            var data = new Models.News.Article.PageLoad();

            using (SqlDataReader dr = DB.RunDataReader(@"
SELECT TOP 1 P.* 
FROM News.News P
WHERE P.Title = @Title
ORDER BY P.ReleaseDate DESC
                ",
                CommandType.Text,
                DB.CreateParameter("@Title", title)))
            {
                if (dr.HasRows)
                {
                    dr.Read();
                    data.Article = DB.InitFromReader<Shared.Entities.News.Article>(dr, new ColMap { NameInDb = "ArticleID", NameInObject = "ID" });
                }

                return data;
            }
        }
    }
}
